Function.prototype.mycall = function(context = window) {
    if(typeof this != 'function') {
        console.log('不是一个函数');
        return
    }
    let arg = [...arguments].slice(1)
    context.fn = this
    let result = context.fn(...arg)
    delete context.fn
    return result
}

// let obj = {
//     a: 1
//   };
//   function fn(name, age) {
//     console.log(this.a); //1
//     console.log(name);
//     console.log(age);
//   }
//   fn.mycall(obj, "我是 lihua", "18");


Function.prototype.myapply = function(context = window, args) {
    if(typeof this != 'function') {
        console.log('不是一个函数');
        return
    }
    

    context.fn = this
    let result = context.fn(...args)
    delete context.fn
    return result
}

// let obj = {
//     a: 1
//   };
//   function fn(name, age) {
//     console.log(this.a); //1
//     console.log(name);
//     console.log(age);
//   }
//   fn.myapply(obj, ["我是 lihua", "18"]);


  Function.prototype.mybind = function(context = window) {
    if(typeof this != 'function' || this == null) {
        throw new TypeError('错误')
    }
    let self = this
    let args = [...arguments].splice(1)
    return function fn(...arguments) {
        return self.apply(context, args.concat(arguments))
    }
  }

  let obj = {
    a: 1
  };
  function fn(name, age, test) {
    console.log(this.a); //1
    console.log(name);
    console.log(age);
    console.log(test);
  }
  fn.mybind(obj,['1', '2'])(3);

